﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using mitasoft.ExcelHelper;
using Microsoft.Office.Interop.Excel;
using RVEAd.Business;

namespace RVEAd
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void btnStripCatalogClient_Click(object sender, EventArgs e)
        {
            //Forms.FormCatalogClienti frmCatalogClienti = new Forms.FormCatalogClienti();
            //frmCatalogClienti.MdiParent = this;
            //frmCatalogClienti.Show();

            ShowForm(new Forms.FormCatalogClienti());
        }

        private void ShowForm(Form f)
        {

            CloseAllOpenedWindows();

            f.WindowState = FormWindowState.Maximized;
            f.MdiParent = this;
            f.Show();
        }

        private void CloseAllOpenedWindows()
        {
            foreach (var form in this.MdiChildren)
            {
                form.Close();
            }
        }

        private void btnStripPlanifica_Click(object sender, EventArgs e)
        {
            ShowForm(new Forms.FormPlanifica());
        }

        private void btnVizualizareProgramari_Click(object sender, EventArgs e)
        {
            ShowForm(new Forms.FormVizualizarePlanificare());
        }

        private void btnCatalogCategorii_Click(object sender, EventArgs e)
        {
            ShowForm(new Forms.FormCatalogCategorii());
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            ShowForm(new Forms.FormReportChooser());
        }

        private void button1_Click(object sender, EventArgs e)
        {



        }

        private void toolStripButtonAutoPlan_Click(object sender, EventArgs e)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendLine("ATENTIE !");
            builder.AppendLine("Prin planificare automata se vor pierde");
            builder.AppendLine(string.Format("toate planificarile existente pe luna: {0} si anul: {1} !!!", DateTime.Now.Month, DateTime.Now.Year));

            if (MessageBox.Show(builder.ToString(), "ATENTIE", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.Yes)
            {
                try
                {
                    RveBL rveBL = new RveBL();
                    rveBL.RandomizePlans(DateTime.Now.Year, DateTime.Now.Month);

                    MessageBox.Show("Planificare automata incheiata cu succes"
                     , "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Eroare la planificare automata " + Environment.NewLine + ex.ToString()
                        , "Eroare", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }


            }
        }
    }

    public class ExportToExcel
    {
        public static void Export(int year, int month)
        {
            // do the headers
            var engine = new Engine();
            var sheet = engine.OpenWorksheet("Template1.xls");

            var tempDayNumber = sheet.GetCell(1, 1);
            var tempDayName = sheet.GetCell(1, 2);
            var tempDaySundayName = sheet.GetCell(1, 3);
            var tempAMInterval = sheet.GetCell(1, 11);
            var tempPMInterval = sheet.GetCell(1, 12);

            var startRow = 3;
            var startCol = 1;


            List<Cell> cells = new List<Cell>();

            // build days header
            DaysHeader(year, month, tempDayNumber, tempDayName, tempDaySundayName, startRow, startCol, cells);

            // days header deja o adaugat 2 randuri
            startRow = startRow + 2;

            for (int c = 0; c < 2; c++)
            {
                for (int i = 0; i < 5; i++)
                {
                    var cellClientCrt = new Cell
                    {
                        RowIndex = startRow + c,
                        ColIndex = startCol,
                        Value = "",
                    };
                    cells.Add(cellClientCrt);

                    var cellClientName = new Cell
                    {
                        RowIndex = startRow + c,
                        ColIndex = startCol + 1,
                        Value = "Client" + c.ToString(),
                    };
                    cells.Add(cellClientName);

                    var cellInterval = new Cell
                    {
                        RowIndex = startRow + c,
                        ColIndex = startCol + 2,
                        Value = Helpers.Common.GetIntervalById(i),
                        MakeAs = Helpers.Common.IsAM(i) ? tempAMInterval : tempPMInterval
                    };
                    cells.Add(cellInterval);

                    startRow++;
                }

            }


            sheet.WriteToExcel(() => { return cells; });

            //RveBL rveBl = new RveBL();

            //foreach (var client in rveBl.GetClients())
            //{
            //    var adPlans = rveBl.GetPlanForExport(2011, 4, client.ID);
            //}


        }

        private static void DaysHeader(int year, int month, Range tempDayNumber, Range tempDayName, Range tempDaySundayName, int startRow, int startCol, List<Cell> cells)
        {
            // las spatiu pentru coloane in fata (nr crt, nume client, interval)
            startCol = startCol + 3;

            var monthDays = Helpers.Common.GetMonthDays(new DateTime(year, month, 1));

            for (int i = 0; i < monthDays.Count; i++)
            {
                var dayOfWeek = monthDays[i].DayOfWeek;

                var cellDay = new Cell
                {
                    RowIndex = startRow,
                    ColIndex = startCol + i,
                    Value = monthDays[i].Day,
                    MakeAs = tempDayNumber
                };
                cells.Add(cellDay);

                var cellDayName = new Cell
                {
                    RowIndex = startRow + 1,
                    ColIndex = startCol + i,
                    Value = Helpers.Common.TranslateDayName(dayOfWeek),
                    MakeAs = dayOfWeek == DayOfWeek.Sunday ? tempDaySundayName : tempDayName
                };
                cells.Add(cellDayName);
            }
        }
    }
}
